<template>
  <div>
    <div class="demo-input-size">
      <el-select
        v-model="searchMode"
        placeholder="请选择"
        style="width: 150px; margin-right: 10px"
      >
        <el-option value="id" label="用户id"></el-option>
        <el-option value="username" label="账号"></el-option>
        <el-option value="nickname" label="昵称"></el-option>
      </el-select>
      <el-input
        v-if="searchMode === 'id'"
        placeholder="请输入用户id"
        prefix-icon="el-icon-search"
        style="width: 250px; padding-right: 5px"
        v-model="searchParams.id"
      ></el-input>
      <el-input
        v-if="searchMode === 'username'"
        placeholder="请输入账号"
        prefix-icon="el-icon-search"
        style="width: 250px; padding-right: 5px"
        v-model="searchParams.username"
      ></el-input>
      <el-input
        v-if="searchMode === 'nickname'"
        placeholder="请输入昵称"
        prefix-icon="el-icon-search"
        style="width: 250px; padding-right: 5px"
        v-model="searchParams.nickname"
      ></el-input>
      <el-button type="primary" @click="search">搜索</el-button>
      <el-button type="warning" @click="reload">重置</el-button>
    </div>

    <div style="padding-top: 10px">
      <el-button type="primary" @click="handleAdd" style="font-size: 18px"
        >新增</el-button
      >
      <el-button type="danger" @click="delBatch" style="font-size: 18px"
        >批量删除</el-button
      >
    </div>

    <el-dialog v-model="dialogFormVisible" :title="dialogTitle">
      <el-form label-width="50px" style="padding: 0 60px">
        <el-form-item label="账号" v-if="dialogTitle == '新增用户'">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="dialogTitle == '新增用户'"
          >123456</el-form-item
        >
        <el-form-item label="昵称">
          <el-input v-model="user.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份">
          <el-select v-model="user.role" placeholder="请选择">
            <el-option
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="user.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="user.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogFormVisible = false" style="font-size: 20px"
          >取消</el-button
        >
        <el-button type="primary" @click="save" style="font-size: 20px"
          >确定</el-button
        >
      </template>
    </el-dialog>

    <el-table
      :data="tableData"
      background-color="black"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column
        prop="username"
        label="账号"
        width="150"
      ></el-table-column>
      <el-table-column label="身份" width="150">
        <template #default="scope">
          <span v-if="scope.row.role === 'user'">用户</span>
          <span v-if="scope.row.role === 'admin'">管理员</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="昵称"
        width="180"
      ></el-table-column>
      <el-table-column prop="phone" label="电话" width="180"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        width="350"
      ></el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="scope">
          <el-button
            type="success"
            @click="handleEdit(scope.row)"
            style="font-size: 18px"
            >编辑</el-button
          >
          <el-button
            type="danger"
            @click="handleDelete(scope.row.id)"
            style="font-size: 18px"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="flex: 0 0 auto">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[3, 5, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentPage"
      ></el-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useStore } from "vuex";
import md5 from "js-md5";
import { ElMessageBox } from "element-plus";

const store = useStore();
const md5Fn: any = md5;
// Reactive data
const tableData = ref<any[]>([]);
const roleOptions = ref([
  { value: "admin", label: "管理员" },
  { value: "user", label: "用户" },
]);
const total = ref<number>(0);
const pageSize = ref<number>(5);
const currentPage = ref<number>(1);
const searchMode = ref<string>("id");
const searchParams = ref({
  id: "",
  username: "",
  nickname: "",
});
const dialogFormVisible = ref<boolean>(false);
const dialogTitle = ref<string>("");
const user = ref<any>({
  username: "",
  nickname: "",
  newPassword: "",
  role: "",
  phone: "",
  email: "",
  address: "",
});
const multipleSelection = ref<any[]>([]);

// Methods
const handleSizeChange = (size: number) => {
  pageSize.value = size;
  load();
};

const handleCurrentPage = (page: number) => {
  currentPage.value = page;
  load();
};

const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
};

const load = () => {
  store.state.request
    .get("/user/page", {
      params: {
        pageNum: currentPage.value,
        pageSize: pageSize.value,
        id: searchParams.value.id,
        username: searchParams.value.username,
        nickname: searchParams.value.nickname,
      },
    })
    .then((res: any) => {
      if (res.code === "200") {
        tableData.value = res.data.records;
        total.value = res.data.total;
      }
    });
};

const search = () => {
  currentPage.value = 1;
  load();
};

const reload = () => {
  searchParams.value = { id: "", username: "", nickname: "" };
  load();
};

const save = () => {
  if (dialogTitle.value === "新增用户") {
    if (user.value.username.trim() === "") {
      store.state.message.error("账号不能为空");
      return;
    }
    user.value.newPassword = md5Fn("123456");
  }
  if (user.value.nickname.trim() === "") {
    store.state.message.error("昵称不能为空");
    return;
  }
  if (user.value.role.trim() === "") {
    store.state.message.error("身份不能为空");
    return;
  }
  if (user.value.phone.trim() === "") {
    store.state.message.error("电话不能为空");
    return;
  }
  if (user.value.email.trim() === "") {
    store.state.message.error("邮箱不能为空");
    return;
  }

  store.state.request.post("/user", user.value).then((res: any) => {
    if (res.code === "200") {
      store.state.message.success("保存成功");
      dialogFormVisible.value = false;
      load();
    } else {
      store.state.message.error(res.msg);
    }
  });
};

const handleAdd = () => {
  dialogTitle.value = "新增用户";
  dialogFormVisible.value = true;
  user.value = {
    username: "",
    nickname: "",
    newPassword: "",
    role: "",
    phone: "",
    email: "",
    address: "",
  };
};

const handleEdit = (row: any) => {
  user.value = JSON.parse(JSON.stringify(row));
  dialogTitle.value = "编辑用户";
  dialogFormVisible.value = true;
};

const handleDelete = (id: any) => {
  ElMessageBox.confirm("确认删除该用户吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    store.state.request.delete("/user/" + id).then((res: any) => {
      if (res.code === "200") {
        store.state.message.success("删除成功");
        load();
      } else {
        store.state.message.error(res.msg);
      }
    });
  });
};

const delBatch = () => {
  const ids = multipleSelection.value.map((v) => v.id);
  ElMessageBox.confirm("确认删除这些用户吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    store.state.request.post("/user/del/batch", ids).then((res: any) => {
      if (res.code === "200") {
        store.state.message.success("删除成功");
        load();
      } else {
        store.state.message.error(res.msg);
      }
    });
  });
};

// Lifecycle
onMounted(() => {
  console.log("数据结构qw");

  load();
});
</script>

<style scoped></style>
